package com.shopyourstyle.activities;

import android.content.Context; 
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class MagView implements SurfaceHolder.Callback,OnTouchListener {

	//For maintain the surface 
	private int width , height;
	private float scale = 1.0f;
	private Paint textPaint = new Paint();
	private static final String START_TEXT = "MagView Start";
	private static final String PACKAGE_NAME = "ShopStyle.htc" ;
	
	private Context _context;
	private Drawable pic1;
	private Drawable _picBar;
	private SurfaceView _sv;
	
	public String backgroundName;
	public MagItem[] items;
	private Drawable _picBackground;
	private int _heightBackground;
	private int _widthBackground;
	
	private boolean _editMode;
	private Paint _textPaint = new Paint();
	private int _textHeight;
	private int myIndex;
	private boolean _inActive;
	
	public void setInActive(boolean inActive) {
		_inActive = inActive;
	}
	
	public void setEditable(boolean editMode) {
		_editMode = editMode;
	}
	
	public void setSurfaceView(SurfaceView sv) {
		_sv = sv;
		_sv.setOnTouchListener( null );
		_sv.setOnTouchListener( this );
		_inActive = true;
		
	}
	public MagView(Context context, SurfaceView sv) {
		setSurfaceView ( sv );
		SurfaceHolder holder = _sv.getHolder();
		holder.addCallback(this);

		_inActive = false;
		_editMode = false;
		
		//super(context);
		_context= context;

		
		//Initialize MagItem
		items = new MagItem[6];	//Maximum for 6 product.
		for (int i=0;i<items.length ;++i)
			items[i] = null;
		
		//Editing related
		_textPaint.setColor( Color.GRAY  );
		_textPaint.setTextSize(20);
		_textHeight = (int) ( _textPaint.getTextSize() );
		
	}
	

	public void setBar(String barFilename) {
		int resID = _context.getResources().getIdentifier( barFilename , "drawable", _context.getPackageName() );
		_picBar = _context.getResources().getDrawable( resID );
		
	}	
	public void setBackground(String backgroundFilename) {
		
		int resID = _context.getResources().getIdentifier( backgroundFilename , "drawable", _context.getPackageName() );
		_picBackground = _context.getResources().getDrawable( resID );
		if (_picBackground!=null) {
			//Log.e("MagItem" , "Allocated resource [" + backgroundFilename + "]" + _picBackground.toString());
			_heightBackground = _picBackground.getIntrinsicHeight();
			_widthBackground = _picBackground.getIntrinsicWidth();
			//Log.e("MagItem" , "  H[" + _heightBackground + "] W[" + _widthBackground + "]" );
			_picBackground.setBounds(0, 0, _widthBackground , _heightBackground);
			_picBackground.setAlpha(240);
		}
		else {
			Log.e("MagItem" , "Allocated resource [" + backgroundFilename + "] Failed" );
			
		}
	}
	public boolean addMagItem( MagItem m ) {
		int i;
		for (i=0;i<items.length ;++i) {
			if (items[i]==null) //Avaliable
				break;
		}
		if (i==items.length ) return false;	//Fail to add new MagItem
		
		//Log.e ("@addMagItem", "index :" + i );
		items[i] = m;
		return true;
	}
	
	public void redraw() {
		if (!this._inActive) return ; //Not your business..
		
		Canvas c = _sv.getHolder().lockCanvas();
		if (c == null) return ;
		
		c.drawColor(Color.BLACK);	//Clear the background

		//Display background
		if (_picBackground!=null)
			_picBackground.draw(c);
		
		//Display items
		//MagItem mtest = new MagItem( _context , "ship", "Line1" , "Line2" , "Line3");
		//mtest.drawOnCanvas(c, 300, 300, 255);
		//mtest.drawOnCanvas(c, 370, 340, 128);
		for (int i=0;i<items.length ;++i ) {
			if (items[i]==null) break;
			//Log.e("@drawItem" , i + "  x:" +items[i]._x + " , y:" + items[i]._y );
			items[i].drawOnCanvas(c);
			//mtest.drawOnCanvas(c, 300, 300, 255);
			//getHolder().unlockCanvasAndPost(c);
		}

		
		//Display the bar
		if (_picBar!=null) {
			_picBar.setBounds( 0 , 0 , 
					_picBar.getIntrinsicWidth(),
					_picBar.getIntrinsicHeight());
			_picBar.setAlpha( 200 );
			_picBar.draw(c);
		}
		
		
		//Draw edit page option.
		String tmpStr;
		if (_editMode) 	tmpStr = new String( "    In Edit" );
		else			tmpStr = new String( "NOT In Edit" );
		/*
		c.drawText( tmpStr , 
					width - _textPaint.measureText( tmpStr ) ,
					_textHeight ,
					_textPaint);
		*/

		c.drawText( tmpStr ,
				
				480  - _textPaint.measureText( tmpStr ) ,
				_textHeight ,
				_textPaint);
		
		_sv.getHolder().unlockCanvasAndPost(c);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,	int height) {
		if (_inActive==false) return ;	//Not your business
		// TODO Auto-generated method stub
		this.width = width;
		this.height = height;
		if (width > height) {
			this.scale = width / 480f;
		} else {
			this.scale = height / 480f;
		}
		redraw();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (!this._inActive) return true;
		//Log.e ( "@OnTouch" , "Hahaha..");
		
		// TODO Auto-generated method stub
		int pointerCount = event.getPointerCount();
		//I assume only "one" click on the screen...
		//Log.e("@onTouchEvent" , "..start." + event.getAction() );
		if (pointerCount>1) return true;
		
		int actionCode = event.getAction() & MotionEvent.ACTION_MASK;
		
		switch ( actionCode  ) {   
	        case MotionEvent.ACTION_DOWN:		
        		//Log.e("@onTouchEvent" , "...");

	        	myIndex = -1;
	        	for (int i=0; i<items.length;++i) {
	        		//Check if we were in this item ?
	        		if (items[i]==null) break;
	        		int localX = (int) event.getX(0) - items[i].getX();
	        		int localY = (int) event.getY(0) - items[i].getY();
	        		if ( items[i].checkEvent(localX, localY , "item" + i , !_editMode)) {
	        			myIndex = i;
	        			break;
	        		}
	        	}
	        	break;
	        case MotionEvent.ACTION_MOVE :
	        	//Moving object, this will only avaliable in edit mode.
	        	if (_editMode && myIndex>=0) {
	        		items[myIndex].dragTo( (int)event.getX(0) ,  (int)event.getY(0) );
	        		redraw();
	        	}
	        	
	        	break;
	        case MotionEvent.ACTION_UP:
	        	redraw();
	        	break;
		}

		return true;
	}
	
	

}
